package com.tlias.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tlias.Service.EmpService;
import com.tlias.mapper.EmpMapper;
import com.tlias.pojo.Emp;
import com.tlias.pojo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    //EmpMapper依赖注入
    @Autowired
    private EmpMapper empMapper;

    /*
     * 分页查询
     * */
    /*@Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 调用empMapper方法分页查询
        Integer start = (page - 1) * pageSize; //获取开始索引
        List<Emp> emplist = empMapper.page(start, pageSize);

        //2. 调用empMapper方法统计总数
        Long count = empMapper.count();

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(count,emplist);
        return pageBean;
    }*/

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);

        //执行查询
        List<Emp> empList = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) empList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    /*
     * 批量删除
     * */
    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    /*
     * 新增员工
     * */
    @Override
    public void insert(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    /*
     * 根据ID查询员工
     * */
    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    /*
     * 更改员工数据
     * */
    @Override
    public void update(Emp emp) {
        //补充基础数据,更新修改时间为当前时间
        emp.setUpdateTime(LocalDateTime.now());
        //调用Mapper方法进行修改
        empMapper.update(emp);
    }

    /*
    * 用户登录
    * */
    @Override
    public Emp login(Emp emp) {
        return empMapper.selectByIdAndUserName(emp);
    }



}
